package com.itheima.service.impl;

import com.itheima.pojo.Question;
import com.itheima.repository.QuestionsRepository;
import com.itheima.service.QuestionService;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liangWeiQUQ
 * @Description: TODO
 * @DateTime: 2021/3/28 11:38
 **/
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionsRepository questionsRepository;

    /**
       * @Author: 传闻中的梁大侠
       * @Date: 14:52 2021/3/28
       * @Parms [question]
       * @ReturnType: com.itheima.pojo.Question
       * @Description: 添加问题信息
     */
    @Override
    public Question add(Question question) {
        Question question2 = questionsRepository.save(question);
        return question2;
    }

    @Override
    public List<Question> findByAnswer(String title) {
        List list = new ArrayList();
        //1.构建QueryBuilder对象设置查询类型和查询条件
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title);
        //2.调用goods仓库search方法进行查询
        Iterable<Question> search = questionsRepository.search(queryBuilder);
        for (Question question : search) {
            list.add(question);
        }
        return list;
    }
}
